package cn.edu.ctbu.myblog.webapi;

import cn.edu.ctbu.myblog.domain.UserDO;
import cn.edu.ctbu.myblog.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/webapi/login")
public class LoginRestController {
    @Autowired
    UserService userService;


    @GetMapping("/user")
    public int user(HttpServletRequest htttprequest,String userName,String userPassword){
        List<UserDO> userDOS=userService.findByNameAndPassword(userName,userPassword);

        if(userDOS.size()>0){
            HttpSession session=htttprequest.getSession();
            session.setAttribute("userId",userDOS.get(0).getId());
            session.setAttribute("user",userDOS.get(0));
            return 1;//成功

        }
        return -1;//不存在用户
    }



    @RequestMapping("/myInformation")
    public List<UserDO> getMyInformation(HttpServletRequest htttprequest){
        HttpSession session=htttprequest.getSession();
        int userId=(int)session.getAttribute("userId");
        List<UserDO> userDOS=userService.getById(userId);

        return userDOS;
    }
}
